package com.edu.app.mapper.linkmappers;

import com.edu.app.bean.core.SysCustomer;
import com.edu.app.bean.core.SysUser;
import com.edu.app.bean.linkbeans.SysUserCustomer;
import com.edu.app.util.AppBaseMapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface SysUserCustomerMapper extends AppBaseMapper<SysUserCustomer> {
    @Select("insert ignore into s_user_customer(create_by, create_time, update_by, update_time, user_id, customer_id, rolePlay) values ${data}")
    Integer assignCustomers(@Param("data") String data);

    @Select("SELECT DISTINCT t3.* FROM S_USER t1 INNER JOIN S_USER_CUSTOMER t2 on t1.ID = t2.USER_ID INNER " +
            "JOIN S_CUSTOMER t3 on t2.CUSTOMER_ID = t3.ID " +
            "WHERE t1.id = #{id}")
    List<SysCustomer> getCustomerByUserId(Integer id);

    @Select("SELECT t1.* FROM S_USER t1 INNER JOIN S_USER_CUSTOMER t2 on t1.ID = t2.USER_ID INNER " +
            "JOIN S_CUSTOMER t3 on t2.CUSTOMER_ID = t3.ID " +
            "WHERE t3.id = #{id}")
    List<SysUser> getUserByCustomerId(Integer id);

    @Select("SELECT DISTINCT t1.* FROM S_USER t1 INNER JOIN S_USER_CUSTOMER t2 on t1.ID = t2.USER_ID INNER " +
            "JOIN S_CUSTOMER t3 on t2.CUSTOMER_ID = t3.ID INNER JOIN S_USER_ROLE t4 ON t4.user_id = t1.id " +
            "INNER JOIN S_ROLE t5 on t5.ID = t4.ROLE_ID " +
            "WHERE t3.ID = #{id} AND t5.ROLE_NAME = #{role_name} and t2.ROLEPLAY = #{role_name}")
    List<SysUser> getUserByCustomerIdWithRole(@Param("id") Integer id, @Param("role_name") String role_name);

    @Select("DELETE FROM S_USER_CUSTOMER WHERE customer_id = #{customer_id}")
    Integer deleteUserCustomerByCustomerId(Integer activity_id);

    @Select("DELETE FROM S_USER_CUSTOMER WHERE user_id = #{user_id}")
    Integer deleteUserCustomerByUserId(Integer user_id);

    @Select("SELECT DISTINCT t3.* FROM S_USER t1 INNER JOIN S_USER_CUSTOMER t2 on t1.ID = t2.USER_ID INNER " +
            "JOIN S_CUSTOMER t3 on t2.CUSTOMER_ID = t3.ID " +
            "WHERE t1.username = #{username} and t3.status = 1 ORDER BY t3.create_time DESC")
    List<SysCustomer> getUserCustomerByPage(String username);

    @Select("SELECT DISTINCT t3.* FROM S_USER t1 INNER JOIN S_USER_CUSTOMER t2 on t1.ID = t2.USER_ID INNER " +
            "JOIN S_CUSTOMER t3 on t2.CUSTOMER_ID = t3.ID " +
            "WHERE t1.username = #{username} and t3.status = 0 ORDER BY t3.create_time DESC")
    List<SysCustomer> getUserPoCustomerByPage(String username);
}
